package processes;

import java.sql.SQLException;

import DAO.ProductDAO;
import to.MaterialTO;
import to.OrderTO;
import to.OutcomeTO;
import to.ProductTO;
import util.MaterialParser;
import util.ProductParser;

public class TestingMachine extends Machine{
	private ProductDAO dao = ProductDAO.getInstance();
	
	public TestingMachine(MachineManager machineManager, String machineId) {
		super(3500,3000, machineManager, machineId);
		setLineName("Testing Machine");
	}
	
	@Override
	public void beforeManufacture() {		
	}
	
	@Override
	public void afterManufacture() {
		ProductParser pp = ProductParser.getInstance();
		currentProduct.setCuttingQuality(generateQuality());
		pp.writeProduct(machineManager.getNextDir(), currentProduct);
		OutcomeTO outcome = checkProduct(currentProduct);
		sendOutcome(outcome);
	}
	
	public OutcomeTO checkProduct(ProductTO product){
		OutcomeTO outcome = new OutcomeTO();
		outcome.setOrderId(work.getOrderId());
		outcome.setProductId(product.getProductId());
		
		outcome.setCuttingQuality(product.getCuttingQuality());
		outcome.setSewingQuality(product.getSewingQuality());
		outcome.setDyeingQuality(product.getDyeingQuality());
		outcome.setPressingQuality(product.getPressingQuality());
		
		if(outcome.getCuttingQuality()==0){
			outcome.setCuttingError("제단 불량");
			outcome.setCuttingPassed("F"); 
		}else{
			outcome.setCuttingPassed("T");
		}
		
		if(outcome.getSewingQuality()==0){
			outcome.setSewingError("재봉 불량");
			outcome.setSewingPassed("F"); 
		}else{
			outcome.setSewingPassed("T");
		}
		
		if(outcome.getDyeingQuality()==0){
			outcome.setDyeingError("염색 불량");
			outcome.setDyeingPassed("F"); 
		}else{
			outcome.setDyeingPassed("T");
		}
		
		if(outcome.getPressingQuality()==0){
			outcome.setPressingError("다림 불량");
			outcome.setPressingPassed("F"); 
		}else{
			outcome.setPressingPassed("T");
		}
		
		return outcome;
	}
	
	public void sendOutcome(OutcomeTO outcome){
		try {
			dao.insertOutcome(outcome);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		ProductParser pp = ProductParser.getInstance();
		if(outcome.getCuttingPassed().equals("F") || outcome.getSewingPassed().equals("F")
			|| outcome.getDyeingPassed().equals("F") || outcome.getPressingPassed().equals("F")){
			pp.writeProduct("cutting", currentProduct);
		}else{
			pp.writeProduct(machineManager.getNextDir(), currentProduct);
		}
	}
}
